package com.yanqu.road.dao.impl.config;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;

public class TaskDaoImpl extends TempDao {

	public Map<Integer, TaskInfo> getTaskInfoMap() {
		Map<Integer, TaskInfo> infos = new LinkedHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_task order by id asc";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TaskInfo taskInfo = new TaskInfo();
				taskInfo.setTaskId(rs.getInt("id"));
				taskInfo.setType(rs.getInt("type"));
				taskInfo.setLittleType(rs.getInt("littleType"));
				taskInfo.setIntegral(rs.getInt("integral"));
				taskInfo.setPreTaskId(rs.getInt("preTaskId"));
				taskInfo.setNextTaskId(rs.getInt("nextTaskId"));
				taskInfo.setBeginTime(rs.getTimestamp("beginTime"));
				taskInfo.setEndTime(rs.getTimestamp("endTime"));
				taskInfo.setSort(rs.getInt("sort"));
				//密卷任务奖励会根据密卷的期数用|隔起来
				String reward = rs.getString("rewards");
				taskInfo.setRewardStr(reward);
				List<String> rewardList = StringUtils.stringToStringList(reward, "\\|");
				if (rewardList.size() > 1) {
					List<Property> rewardProList = new ArrayList<>();
					for (int i = 0; i < rewardList.size(); i++) {
						rewardProList.add(i, PropertyHelper.parseStringToProperty(rewardList.get(i)));
					}
					taskInfo.setRewardList(rewardProList);
				} else {
					taskInfo.setReward(PropertyHelper.parseStringToProperty(reward));
				}

				infos.put(taskInfo.getTaskId(), taskInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}

		return infos;
	}

	public Map<Integer, TaskConditionInfo> getTaskConditionInfoMap() {
		Map<Integer, TaskConditionInfo> infos = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_task_condition";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TaskConditionInfo conditionInfo = new TaskConditionInfo();
				conditionInfo.setTaskId(rs.getInt("id"));
				conditionInfo.setType(rs.getInt("completeType"));
				conditionInfo.setCanSelect(rs.getBoolean("selectable"));
				String completeParams = rs.getString("completeParams");
				conditionInfo.setCompleteParams(completeParams);
				conditionInfo.setParamList(StringUtils.stringToBigIntegerList(completeParams, "\\|"));
				conditionInfo.setSatisfyType(rs.getInt("satisfyType"));

				infos.put(conditionInfo.getTaskId(), conditionInfo);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return infos;
	}
}
